package com.momoauth.api_client.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.momoauth.api_client.entity.Department;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface DepartmentMapper extends BaseMapper<Department> {

    /**
     * 部门列表
     * @param page
     * @return
     */
    @Select("SELECT deptId,deptName,createTime,status," +
            "(SELECT deptName FROM department WHERE deptId = d.parentId) AS parentName " +
            "FROM department AS d ${ew.customSqlSegment}")
    List<Department> getDepartmentList(Page<Department> page, @Param(Constants.WRAPPER) QueryWrapper wrapper);

    /**
     * 删除部门
     * @param deptId
     * @return
     */
    @Update("UPDATE department SET isDelete = 1 WHERE deptId = #{deptId}")
    Integer deleteDepartmentById(Long deptId);

    /**
     * 查询是否存在子级部门
     * @param parentId
     * @return
     */
    @Select("SELECT 1 FROM department WHERE parentId = #{parentId} AND isDelete = 0 LIMIT 1")
    Integer existChildrenDeptByParentId(Long parentId);
}
